package connectionpool.internal;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/*****************************************************************************
 * @author amahmood
 * 
 *         Maintains connection information.
 *         Creates a single connection.
 *         Package level visibility
 ****************************************************************************/
class ConnectionManager
{
    private String connectionURL;
    private String userName;
    private String password;

    /*****************************************************************************
     * @param properties
     ****************************************************************************/
    ConnectionManager(Properties properties)
    {
        CommonUtils.checkPreConditionNotNull("DbProperties", properties);

        this.connectionURL = properties.getProperty(DbPropertyNames.URL.name());
        this.userName = properties.getProperty(DbPropertyNames.USERNAME.name());
        this.password = properties.getProperty(DbPropertyNames.PASSWORD.name());
    }

    /*****************************************************************************
     * @param url
     * @param user
     * @param password
     ****************************************************************************/
    ConnectionManager(String url, String user, String password)
    {
        CommonUtils.checkPreConditionNotNull("url", url);
        CommonUtils.checkPreConditionNotNull("user", user);
        CommonUtils.checkPreConditionNotNull("password", password);

        this.connectionURL = url;
        this.userName = user;
        this.password = password;
    }

    /*****************************************************************************
     * @return
     * @throws SQLException
     ****************************************************************************/
    java.sql.Connection getConnection() throws SQLException
    {

        Connection out = DriverManager.getConnection(connectionURL, userName, password);
        return out;
    }

    /*****************************************************************************
     * @return
     * @throws SQLException
     ****************************************************************************/
    java.sql.Driver getDriver() throws SQLException
    {
        Driver out = DriverManager.getDriver(connectionURL);
        return out;
    }

}
